import { NodeTypes } from "../ast";
import { isText } from "./utils";

export function transformText(node) {
    if(node.type === NodeTypes.ELEMENT) {
        return () => {
            const { children } = node; 
            let currentContainer
            for (let i = 0; i < children.length; i++) {
                const child = children[i];
                if(isText(child)) {
                    // 看i的下一个是不是插值或者text
                    for (let j = i+1; j < children.length; j++) {
                        const next = children[j];
                        if(isText(next)) {
                            // 如果是，则进行收集
                            if(!currentContainer) {
                                currentContainer = children[i] = {
                                    type:NodeTypes.COMPOUND_EXPRESSION,
                                    children:[child]
                                }
                            } 
                            currentContainer.children.push(" + ")
                            currentContainer.children.push(next)
                            children.splice(j,1)
                            j--;
                        } else {
                            currentContainer = undefined
                            break
                        }
                    }
                }
            } 
        }
    }
}